var express = require("express");
var router = express.Router();

const { getResult, updateList, getList } = require("../../utils/sql");
const authMiddleWare = require("../../middleWare/auth");

const SqlSearch = require("../../utils/sql1");
const dayjs = require("dayjs");

let DATABASE = "daily_task";
let DATABASE1 = "daily_task_total";

let sqlProxy = new SqlSearch(DATABASE);

router.get("/list", authMiddleWare, async function (req, res) {
  let { date } = req.query;
  let propList = ["date", "user_id"];
  let valueList = [date, req.auth];
  const { data } = await sqlProxy.search(propList, valueList);

  data[0].forEach((item) => {
    item.create_time = dayjs(item.create_time).format("YYYY-MM-DD HH:mm:ss");
    item.update_time = dayjs(item.update_time).format("YYYY-MM-DD HH:mm:ss");
  });

  res.send({
    code: 0,
    data: data[0],
    message: "OK",
  });
});

router.post("/add", authMiddleWare, async function (req, res) {
  let { content, date } = req.body;
  if (!content) {
    res.send({
      code: 1,
      message: "Content is required",
    });
    return;
  }
  let propList = ["content", "date", "user_id"];
  let valueList = [content, date, req.auth];
  await sqlProxy.add(propList, valueList);

  getList(DATABASE1, ["date", "user_id"], [date, req.auth]).then(
    async (result) => {
      if (result.length === 0) {
        await getResult({
          sql: `INSERT INTO ${DATABASE1} (date,num,user_id) value(?,?,?)`,
          paramsList: [date, 1, req.auth],
        });
      } else {
        await getResult({
          sql: `UPDATE ${DATABASE1} SET num = num + 1 WHERE date=? AND user_id=?`,
          paramsList: [date, req.auth],
        });
      }

      res.send({
        code: 0,
        message: "Add Successfully",
      });
    }
  );
});

router.put("/update", authMiddleWare, async function (req, res) {
  let { status, id, date } = req.body;
  await updateList(DATABASE, ["status"], [status], id);
  let res2 = await getList(
    DATABASE,
    ["date", "status", "user_id"],
    [date, 0, req.auth]
  );

  let sql = `UPDATE ${DATABASE1} SET status=? WHERE date=? AND user_id=?`;
  let paramsList = [res2.length === 0 ? 1 : 0, date, req.auth];
  await getResult({
    sql,
    paramsList,
  });
  res.send({
    code: 0,
    message: "OK",
  });
});

// total
router.get("/total", authMiddleWare, async function (req, res) {
  let { date } = req.query;
  let sql = `SELECT * FROM ${DATABASE1} WHERE user_id=?`;
  let paramsList = [req.auth];
  if (date) {
    sql += " AND date=?";
    paramsList.push(date);
  }
  let result = await getResult({
    sql,
    paramsList,
  });

  res.send({
    code: 0,
    data: result[0],
    message: "OK",
  });
});

router.get("/thought", authMiddleWare, async function (req, res) {
  let result = await getList(
    DATABASE1,
    ["date", "user_id"],
    [req.query.date, req.auth]
  );
  res.send({
    code: 0,
    data: result[0]?.thought,
    message: "OK",
  });
});

router.put("/thought", authMiddleWare, async function (req, res) {
  let { date, thought } = req.body;

  let result = await getList(DATABASE1, ["date", "user_id"], [date, req.auth]);
  console.log(result);
  if (result && result[0]) {
    let sql = `UPDATE ${DATABASE1} SET thought=? WHERE date=? AND user_id=?`;
    let paramsList = [thought, date, req.auth];
    await getResult({
      sql,
      paramsList,
    });
    res.send({
      code: 0,
      message: "OK",
    });
  } else {
    let sql = `INSERT INTO ${DATABASE1} (date,num,status,thought,user_id) value(?,?,?,?,?)`;
    let paramsList = [date, 0, 0, thought, req.auth];
    await getResult({
      sql,
      paramsList,
    });
    res.send({
      code: 0,
      message: "OK",
    });
  }
});

module.exports = router;
